// 给定 pushed 和 popped 两个序列，每个序列中的 值都不重复，
// 只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时，返回 true；否则，返回 false 。
var validateStackSequences = function (pushed, popped) {
  let stack = [];
  let i = j = 0;
  // 入栈操作
  for (let i = 0; i < pushed.length; i++) {
    stack.push(pushed[i]);
    if (popped[j] === stack[stack.length - 1]) {
      stack.pop();
      j++;
    }
  }
  // 入栈执行完毕
  while (j < popped.length) {
    if (stack[stack.length - 1] === popped[j]) {
      stack.pop();
      j++;
    } else {
      return false;
    }
  }
  return stack.length === 0;
};
// console.log(validateStackSequences([1, 2, 3, 4, 5], [4, 5, 3, 2, 1]));
console.log(validateStackSequences([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]));
console.log(validateStackSequences([8, 2, 1, 4, 7, 9, 0, 3, 5, 6], [1, 2, 7, 3, 6, 4, 0, 9, 5, 8]));